package com.jh.user.business.impl;

import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.CommonsUtil;
import com.jh.user.business.UserBrandJhKeyBusiness;
import com.jh.user.pojo.UserBrandJhKey;
import com.jh.user.repository.UserBrandJhKeyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jh.common.utils.CommonConstants;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.persistence.EntityManager;
import javax.persistence.criteria.*;
import java.net.URI;
import java.util.*;


@Service
public class UserBrandJhKeyBusinessImpl implements UserBrandJhKeyBusiness {

    @Autowired
    private UserBrandJhKeyRepository userBrandJhKeyRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private EntityManager entityManager;

    @Override
    @Transactional
    public void saveUserBrandJhKey(long brandId, String brandName) throws ServiceException {
        String sql = "select * from t_brand_jh_key order by id desc limit 0, 1";
        UserBrandJhKey existUserBrandJhKey = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(UserBrandJhKey.class));
        if (null != existUserBrandJhKey) {
            long id = existUserBrandJhKey.getId();
            List<UserBrandJhKey> userBrandJhKeyList = new ArrayList<>();
            UserBrandJhKey userBrandJhKey = UserBrandJhKey.addUserBrandJhKey(++id, brandId, brandName, CommonConstants.NOTICE);
            userBrandJhKeyList.add(userBrandJhKey);
            userBrandJhKey = UserBrandJhKey.addUserBrandJhKey(++id, brandId, brandName, CommonConstants.NAME_AUTH);
            userBrandJhKeyList.add(userBrandJhKey);
            userBrandJhKey = UserBrandJhKey.addUserBrandJhKey(++id, brandId, brandName, CommonConstants.BANK_AUTH);
            userBrandJhKeyList.add(userBrandJhKey);
            userBrandJhKey = UserBrandJhKey.addUserBrandJhKey(++id, brandId, brandName, CommonConstants.BANK_TAG);
            userBrandJhKeyList.add(userBrandJhKey);
            userBrandJhKey = UserBrandJhKey.addUserBrandJhKey(++id, brandId, brandName, CommonConstants.BANK_ASCRIPTION);
            userBrandJhKeyList.add(userBrandJhKey);
            userBrandJhKeyRepository.save(userBrandJhKeyList);
            entityManager.flush();
        }
    }
}
